package com.peanut.baby.mvp.liveroom;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.LiveMsg;
import com.peanut.baby.model.LiveRoom;
import com.peanut.baby.model.UploadConf;
import com.tencent.mars.xlog.Log;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by anonymouslystudio on 2017/7/17.
 */

public class RoomDiscussFragment extends BaseMessageFragment implements TextView.OnEditorActionListener{
    private static final String TAG = RoomLectureFragment.class.getSimpleName();
    @BindView(R.id.liveroom_discuss_recycler)
    RecyclerView recycler;
    @BindView(R.id.discuss_edit)
    EditText edit;
    @BindView(R.id.discuss_input_container)
    LinearLayout inputContainer;
    Unbinder unbinder;

    private LiveRoomMessageAdapter adapter;
    List<LiveMsg> messages;

    private LiveRoom room;
    public static RoomDiscussFragment newInstance(LiveRoom room) {
        RoomDiscussFragment fg = new RoomDiscussFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("room",room);
        fg.setArguments(bundle);
        return fg;
    }

    private boolean isHoster(){
        if(InitManager.getInstance().getUserId().equals(room.expertId)
                || InitManager.getInstance().getUserId().equals(room.serverId)){
            return true;
        }
        return false;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_liveroom_discuss, null);
        unbinder = ButterKnife.bind(this, rootView);
        edit.setOnEditorActionListener(this);
        Log.d(TAG,"EditorActionListener set");
        return rootView;
    }

    @Override
    public void initData() {

        room = (LiveRoom) getArguments().getSerializable("room");
        Log.d(TAG,"initData.."+room.toString());
        inputContainer.setVisibility(isHoster() ? View.GONE:View.VISIBLE);

        adapter = new LiveRoomMessageAdapter(getMessages());
        recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycler.setAdapter(adapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        Log.d(TAG,"actionId: "+actionId);
        if(actionId == EditorInfo.IME_ACTION_SEND){
            sendTextMessage();
            return true;
        }
        return false;
    }

    /**
     *
     */
    private void sendTextMessage(){
        String text = edit.getText().toString().trim();
        if(TextUtils.isEmpty(text)){
            showToast("请输入要发送的内容");
           return;
        }
        sendTextMessage(LiveMsg.TAB_DISCUSS,text);
    }

    @Override
    public void onMessageGet(List<LiveMsg> newMsgs) {
        Log.d(TAG,"onMessageGet: "+newMsgs.size());
        super.onMessageGet(newMsgs);
        getMessages().addAll(newMsgs);
        if(isActive() && adapter != null){
            adapter.notifyDataSetChanged();
            recycler.scrollToPosition(adapter.getItemCount()-1);

        }
    }

    private List<LiveMsg> getMessages(){
        if(messages == null){
            messages = new ArrayList<>();
        }

        return messages;
    }

    public void onSendMessageSuccess(){
        edit.setText("");
    }

    public void onPictureUploadSuccess(UploadConf config){
        sendPictureMessage(LiveMsg.TAB_DISCUSS,config.getResultFullUrl(),config.localPath);
    }


}
